/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package threads.synchronisation;

/**
 *
 * @author wadi3
 */
public class Compteur2Threads implements Runnable {
    
    private Integer pair=0;
    private Integer change(){
        synchronized(this.pair){
        this.pair++;
        try{
        Thread.currentThread().sleep(1000+(int)Math.floor(Math.random()*3000));}
        catch(Exception e){
            e.printStackTrace();
        }
        this.pair++;}
        return this.pair;
    }
    
    @Override
    public void run() {
    for (int i=0;i<20;i++){
        try{
            System.out.println(">"+Thread.currentThread().getName()+" "+this.change());
            
        }catch(Exception e){
            e.printStackTrace();
        }
    }    
    }
    
    public static void main(String[] args){
       Compteur2Threads c = new Compteur2Threads();
        
       new Thread(c).start();
       new Thread(c).start();
       
    }
    
}
